package com.jiang.daily.Q757;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/11/20 17:21
 */
public class Solution {
    public int intersectionSizeTwo(int[][] intervals) {
        Arrays.sort(intervals,new Comparator<int[]>(){
            public int compare(int[] i1, int[] i2){
                return i2[1]==i1[1]?i2[0]-i1[0]:i1[1]-i2[1];
            }
        });
        int ans = 2;
        int l = intervals[0][1]-1;
        int r = intervals[0][1];
        for(int i = 1 ; i < intervals.length ; i++){
            if(intervals[i][0] <= l){
                continue;
            }else if(intervals[i][0] <= r){
                ans++;
                l = r;
                r = intervals[i][1];
            }else{
                ans += 2;
                r = intervals[i][1];
                l = intervals[i][1]-1;
            }
        }
        return ans;
    }
}
